/*
 *
 * -------------------------------------------------------------------------------------------------
 *                            COPYRIGHT(C) BOCO HEALTH 2016
 * -------------------------------------------------------------------------------------------------
 *
 * =================================================================================================
 *  *HISTORY
 *
 *      Tag                Date            Author           Description
 * =================================================================================================
 *      created            2017/1/17      caodailu        create
 * =================================================================================================
 *
 *
 */

package com.boco.base.view.custom_im;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.mobileim.YWIMKit;
import com.alibaba.mobileim.aop.Pointcut;
import com.alibaba.mobileim.aop.custom.IMChattingPageUI;
import com.alibaba.mobileim.contact.IYWContact;
import com.alibaba.mobileim.conversation.YWConversation;
import com.alibaba.mobileim.conversation.YWP2PConversationBody;
import com.boco.base.App;
import com.boco.base.R;
import com.boco.base.utils.IMCustomHelper;
import com.boco.base.utils.ImStatusBarUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 聊天界面自带提供两种主题的自定义供用户方便的使用，用户可以通过{@link IMCustomHelper ｝中 实现 AdviceBinder.bindAdvice
 * (PointCutEnum.CHATTING_FRAGMENT_UI_POINTCUT, ChattingUICustom.class);
 * 使用该主题的聊天界面自定义风格：文字和图片小猪气泡风格
 * <p>
 * <p>
 * Created by mayongge on 15-9-23.
 */
public class ChattingUICustom extends IMChattingPageUI {


    @BindView(R.id.head_center_text)
    TextView mHeadCenterText;

    @BindView(R.id.head_left_image)
    ImageView mHeadLeftImage;

    private Fragment mFragment;

    public ChattingUICustom(Pointcut pointcut) {
        super(pointcut);
    }

//    /**
//     * 设置消息气泡背景图，需要.9图
//     * @param conversation 当前消息所在会话
//     * @param message      需要设置背景的消息
//     * @param self         是否是自己发送的消息，true：自己发送的消息， false：别人发送的消息
//     * @return  0: 默认背景 －1:透明背景（无背景） >0:使用用户设置的背景图
//     */
//    @Override
//    public int getMsgBackgroundResId(YWConversation conversation, YWMessage message, boolean
// self) {
//        if (true)
//            return super.getMsgBackgroundResId(conversation, message, self);
//        int msgType = message.getSubType();
//        if (msgType == YWMessage.SUB_MSG_TYPE.IM_TEXT || msgType == YWMessage.SUB_MSG_TYPE
// .IM_AUDIO){
//            if (self){
//                return R.drawable.demo_talk_pop_r_bg;
//            } else {
//                return R.drawable.demo_talk_pop_l_bg;
//            }
//        } else if (msgType == YWMessage.SUB_MSG_TYPE.IM_IMAGE){
//            if (self){
//                return R.drawable.demo_talk_pic_pop_r_bg;
//            } else {
//                return R.drawable.demo_talk_pic_pop_l_bg;
//            }
//        } else if (msgType == YWMessage.SUB_MSG_TYPE.IM_VIDEO){
//            if (self){
//                return R.drawable.demo_talk_pic_pop_r_bg;
//            } else {
//                return R.drawable.demo_talk_pic_pop_l_bg;
//            }
//        } else if (msgType == YWMessage.SUB_MSG_TYPE.IM_GEO){
//            if (self){
//                return R.drawable.aliwx_comment_r_bg;
//            } else {
//                return R.drawable.aliwx_comment_l_bg;
//            }
//        } else if (msgType == YWMessage.SUB_MSG_TYPE.IM_P2P_CUS || msgType == YWMessage
// .SUB_MSG_TYPE.IM_TRIBE_CUS){
//            if (self){
//                return -1;
//            } else {
//                return -1;
//            }
//        }
//        return super.getMsgBackgroundResId(conversation, message, self);
//    }

    @Override
    public View getCustomTitleView(Fragment fragment, Context context, LayoutInflater inflater,
                                   YWConversation conversation) {
        View view = inflater.inflate(R.layout.include_head_top, new RelativeLayout(context), false);
        mFragment = fragment;
        ButterKnife.bind(this, view);
        ImStatusBarUtils.initStatusBar(fragment.getActivity());
        String title = "";
        YWP2PConversationBody conversationBody = (YWP2PConversationBody) conversation
                .getConversationBody();
        if (!TextUtils.isEmpty(conversationBody.getContact().getShowName())) {
            title = conversationBody.getContact().getShowName();
        } else {
            YWIMKit imKit = App.getYwimKit();
            IYWContact contact = imKit.getContactService().getContactProfileInfo(conversationBody.getContact().getUserId(), conversationBody.getContact().getAppKey());
            //生成showName，According to id。
            if (contact != null && !TextUtils.isEmpty(contact.getShowName())) {
                title = contact.getShowName();
            }
        }
        //如果标题为空，那么直接使用Id
        if (TextUtils.isEmpty(title)) {
            title = conversationBody.getContact().getUserId();
        }

        mHeadLeftImage.setVisibility(View.VISIBLE);
        mHeadCenterText.setVisibility(View.VISIBLE);
        mHeadCenterText.setText(title);

        return view;
    }

    @OnClick(R.id.head_left_show)
    public void onClick() {
        mFragment.getActivity().finish();
    }


}